package UIFree.controls
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	public class uif_list extends uif_ecomponent {
		protected var _scrollArea: uif_scrollArea;
		protected var _btop: uif_arrowbutton;
		protected var _bbottom: uif_arrowbutton;
		protected var _selectIndex: int;
		
		private	 var _auto_timer: Timer;
		private  var _prevSelectClass: String;
		private	 var _topIndex: int;
		private	 var _itemSize: Number; 
		
		public static var SELECT: String = 'SELECT';
		public static var DEFITEMHEIGHT: Number = 20;
		public static var SELECTEVENT: String = MouseEvent.CLICK;
		
		public function uif_list(parent:uif_component, 
					ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, 
					styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_scrollArea = createAcrollArea();
			createButton(true);
			createButton(false);
			_selectIndex = -1;
		}
		
		override protected function listenEvents(): void {
			addEvent(MouseEvent.MOUSE_WHEEL, doMWell);
		}
		
		protected function doMWell(e: MouseEvent): void {
			if (!_scrollArea.area.getFX('y'))
				_scrollArea.scrollTo(0, e.delta * _itemSize);
		}
		
		protected function createAcrollArea(): uif_scrollArea {
			var inn: Rectangle = getInner();
			return new uif_scrollArea(this, inn.x, inn.y, inn.width - buttonSize(), inn.height, 'box');
		}
		
		public function get selectItem(): uif_ecomponent {
			return (_selectIndex>-1)?(_scrollArea.area.getChildAt(_selectIndex) as uif_ecomponent):null;
		}
		
		public function set selectItem(item: uif_ecomponent): void {
			if (item.parent == _scrollArea.area)
				selectIndex = _scrollArea.area.getChildIndex(item);
		}
		
		public function set selectIndex(newIndex: int): void {
			if ((_selectIndex != newIndex) &&
				(_scrollArea.area.numChildren > newIndex)) {
				if (newIndex > -1) {
					var l_selectItem: uif_component = _scrollArea.area.getChildAt(newIndex) as uif_component;
					if (l_selectItem) {
						if (selectIndex > -1) {
//							selectItem.fx('style', selectItem.style, app.styleByObj(_prevSelectClass)).start();
							selectItem.styleClass = _prevSelectClass;
						}
						_selectIndex = newIndex;
						if (style.selectClass) {
							_prevSelectClass = l_selectItem.styleClass;
//							l_selectItem.fx('style', l_selectItem.style, app.styleByObj(style['selectClass'])).start();
							l_selectItem.styleClass = style.selectClass;
						}
						
						_scrollArea.scrollToItem(l_selectItem);
					}
				} else {
					if (selectIndex > -1) selectItem.styleClass = _prevSelectClass;
					_selectIndex = newIndex;
				}
				this.dispatchEvent(new Event(Event.CHANGE));
				this.dispatchEvent(new Event(uif_list.SELECT));
			}
		}
		
		public function get selectIndex(): int {
			return _selectIndex;
		}
		
		protected function buttonSize(): int {
			return stParam('buttonSize', 20);
		}
		
		private function createButton(top: Boolean): void {
			var inn: Rectangle = _scrollArea.getBounds(this);
			
			var button: uif_arrowbutton = new uif_arrowbutton(this, 
				inn.right, 
				top?inn.y:(inn.bottom - buttonSize()), buttonSize()
				, 
				buttonSize(), top?1:3, stParam('buttonClass', 'button'));
				
			addEvent(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				sButtonDown(top?1:-1);
			}, button);
			
			addEvent(MouseEvent.MOUSE_OUT, function(e: MouseEvent): void {
				if (_auto_timer) {
					_auto_timer.stop();
					_auto_timer = null;
				}
			}, button);
			
			addEvent(MouseEvent.MOUSE_UP, function(e: MouseEvent): void {
				if (_auto_timer) {
					_auto_timer.stop();
					_auto_timer = null;
				}
			}, button);
		}
		
		private function sButtonDown(scrollTo:int): void {
			_scrollArea.scrollTo(0, scrollTo * _itemSize);
			var _wait: int = 2;
			_auto_timer = new Timer(150);
			_auto_timer.addEventListener(TimerEvent.TIMER, function(e: TimerEvent): void {
				if (_wait == 0) {
					_scrollArea.scrollTo(0, scrollTo * _itemSize);
				} else _wait--;
			});
			_auto_timer.start();
		}
		
		public function get list(): Array {
			return _scrollArea.list;
		}
		
		public function getItem(index: int): uif_component {
			return area.getChildAt(index) as uif_component;
		}
		
		public function getItemByName(name: String): uif_component {
			return area.getChildByName(name) as uif_component;
		}
		
		public function get area(): uif_component {
			return _scrollArea.area;
		}
		
		public function get listLength(): int {
			return _scrollArea.list?_scrollArea.list.length:0;
		}
		
		public function clearList(): void {
			if (_scrollArea.list) _scrollArea.list.splice(0, _scrollArea.list.length);
			_scrollArea.area.removeChilds();
			_selectIndex = -1;
			_scrollArea.area.y = 0;
		}
		
		public function removeItem(item: uif_ecomponent): void {
			var sIndex: int = selectIndex; 
			if (item == selectItem) selectIndex = -1;
			_scrollArea.removeItem(item);
			selectIndex = (sIndex < list.length)?sIndex:(list.length - 1);
		}
		
		protected function defaultCreateFunc(area: uif_component, item: Object, index: int): uif_label {
			var inner: Rectangle = area.getInner();
			var _item: uif_label = new uif_label(area, inner.x, inner.y + DEFITEMHEIGHT * index, inner.width, DEFITEMHEIGHT, style.itemClass);
			if (item.caption) _item.text = item.caption; 
			return _item;
		}
		
		public function attachList(list: Array, createFunc: Function=null): uif_list {
			if (createFunc == null) createFunc = defaultCreateFunc;
			function createElem(area: uif_component, item: *, index: int): uif_component {
				var l_item: uif_component = createFunc(area, item, index);
				addEvent(SELECTEVENT, function(e: MouseEvent): void {
					This.selectItem = l_item;
				}, l_item);
				_itemSize += l_item.height;
				return l_item;
			}
			_scrollArea.attachList(list, createElem);
			_selectIndex = -1;
			_itemSize	= this._scrollArea.area.getInner().height / list.length;
			return this;
		}
	}
}